package com.hongxun.modules.eys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongxun.modules.eys.entity.RequstEPointEntity;
import com.hongxun.modules.eys.service.EDeviceBuyService;
import com.hongxun.modules.eys.service.EDeviceOldService;
import com.hongxun.modules.eys.service.EProblemexistService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.eys.entity.EPointEntity;
import com.hongxun.modules.eys.service.EPointService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;


/**
 * 项目点位采集基础表
 *
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-09-18 14:14:27
 */
@RestController
@RequestMapping("/eys/epoint")
public class EPointController {
    @Autowired
    private EPointService ePointService;
    @Autowired
    EDeviceBuyService eDeviceBuyService;
    @Autowired
    EDeviceOldService eDeviceOldService;
    @Autowired
    EProblemexistService eProblemexistService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("eys:epoint:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        String wheres = " 1=1 ";
        if (query.get("pGuid") != null && !query.get("pGuid").toString().equals("")) {
            wheres += " and  p_guid = '" + query.get("pGuid").toString() + "' ";
        }else{
            wheres += " and  1=2 ";
        }
        if (query.get("pPCode") != null && !query.get("pPCode").toString().equals("")) {
            wheres += " and  p_p_code like '%" + query.get("pPCode").toString() + "%' ";
        }
        if (query.get("pType") != null && !query.get("pType").toString().equals("")) {
            wheres += " and  p_p_name = '" + query.get("pType").toString() + "' ";
        }
        if (query.get("voltage") != null && !query.get("voltage").toString().equals("")) {
            wheres += " and  p_p_voltage = '" + query.get("voltage").toString() + "' ";
        }
        if (query.get("range") != null && !query.get("range").toString().equals("")) {
            wheres += " and  p_r_guid = '" + query.get("range").toString() + "' ";
        }


        wheres += " and  p_p_datastate!= '删除' ";
        query.put("filterSql", wheres + " ");
        List<RequstEPointEntity> ePointEntities = new ArrayList<>();
        List<EPointEntity> ePointList = ePointService.queryList(query);
        Map<String, Object> map = new HashMap<>();
        for (EPointEntity e : ePointList) {
            RequstEPointEntity r = new RequstEPointEntity();
            map.put("filterSql", "p_p_guid ='" + e.getGuid() + "'");
            int buyCount = eDeviceBuyService.queryTotal(map);
            int oldCount = eDeviceOldService.queryTotal(map);
            BeanUtils.copyProperties(e, r);
            r.setDeviceCount(buyCount + oldCount);
            map.put("filterSql", "pointguid ='" + e.getGuid() + "'");
            int p = eProblemexistService.queryTotal(map);
            r.setProblemCount(p);
            ePointEntities.add(r);
        }
        int total = ePointService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(ePointEntities, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    @RequestMapping("/select")
    public R select(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        String wheres = " 1=1 ";
        if (query.get("pGuid") != null && !query.get("pGuid").toString().equals("")) {
            wheres += " and  p_guid = '" + query.get("pGuid").toString() + "' ";
        }

        wheres += " and  p_p_datastate != '删除' ";
        query.put("filterSql", wheres + " ");
        List<EPointEntity> ePointList = ePointService.queryList(query);
        return R.ok().put("returnlist", ePointList);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{guid}")
    @RequiresPermissions("eys:epoint:info")
    public R info(@PathVariable("guid") String guid) {
        EPointEntity ePoint = ePointService.queryObject(guid);

        return R.ok().put("ePoint", ePoint);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("eys:epoint:save")
    public R save(@RequestBody EPointEntity ePoint) {
        ePointService.save(ePoint);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("eys:epoint:update")
    public R update(@RequestBody EPointEntity ePoint) {
        ePointService.update(ePoint);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("eys:epoint:delete")
    public R delete(@RequestBody String[] guids) {
        ePointService.deleteBatch(guids);

        return R.ok();
    }

}
